package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.PersonManaged;
import com.aerfafish.spider.neo4j.repository.PersonManagedRepository;
import com.aerfafish.spider.neo4j.spider.service.PersonCompanyRelationService;
import org.neo4j.driver.exceptions.TransientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

@Service
public class PersonCompanyRelationServiceImpl implements PersonCompanyRelationService {

    @Autowired
    PersonManagedRepository personManagedRepository;



    @Override
    public void save(PersonManaged personManaged) throws Exception {
        save(personManaged.getPerson().getCode(), personManaged.getCompany().getCode());
    }

    @Override
    public void save(String personCode, String companyCode) throws Exception {
        if (personCode == null || companyCode == null) {
            throw new Exception("some param is null");
        }
        try {
            personManagedRepository.save(personCode, companyCode);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                personManagedRepository.save(personCode, companyCode);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                personManagedRepository.save(personCode, companyCode);
            }
        }

    }
}
